#include "Arm.h"
#include "Sprite.h"
#include "Box.h"
#include "RSManager.h"
#include "GameDefine.h"
#include "Box.h"
#include "ShareComponents.h"
#include "MainGame.h"
#define  ROTATE_DES 3.14f / 5.0f
Arm::Arm(D3DXVECTOR2 _tam, float r, float pi,float v,float min, float max):Object(new Box(),ObjectType::ObjAnti_DYNAMID)
{
	_centerPoint = _tam;
	_radius = r;
	angle = pi;
	_omega = v;
	_min = min;
	_max = max;
	init();
}


Arm::~Arm(void)
{
}

void Arm::init()
{
	_isParent = false;
	_sprite = RSManager::getInstance()->getSprite(ARM_BODY);
	_box->_position.x = _centerPoint.x + _radius * cos(angle);
	_box->_position.y = _centerPoint.y + _radius * sin(angle);
	_k = 1.0f;
	_next = NULL;
	_state = WAVE;
	_box->_size = _sprite->getRenderSize();
	tokenId = 1;
	token = 0;
	delay = 0;
	this->_hits = 10;
	_parent = NULL;
	_next = NULL;
}

void Arm::update()
{
	if(_hits < 0)
	{		
		_state = DEATH;
		gl_MainGame->removeWorldObject(this);
	}
	if(_parent == NULL)
 	{
		switch (_state)
		{
		case WAVE:
			_box->_position.x = _centerPoint.x + _radius * cos(angle);
			_box->_position.y = _centerPoint.y + _radius * sin(angle);
			increaseAngle();		
			_next->wave(angle,_k,token);
			break;
		case ROTATE:
			_box->_position.x = _centerPoint.x + _radius * cos(angle);
			_box->_position.y = _centerPoint.y + _radius * sin(angle);			
			angle += 0.05f * _k;		
			if(_next!= NULL)
			{		
				_next->rotate(angle - ROTATE_DES * _k,_k);
			}
			break;
		case FOLLOW:
			D3DXVECTOR2 delta = gl_player->_position - _box->_position;
			float agl = atan(delta.y /delta.x);
			if(agl > 0.0f) agl = -3.14f + agl; 
			_next->follow(agl);	
			break;
		}
	}

}

void Arm::render()
{

	_sprite->Render(_box->_position);
	if(_next != NULL) _next->render();
}

Box* Arm::getCollisionBox()
{
	return _colBox;
}

void Arm::addNext(Arm* arm)
{
	_next = arm;
	arm->_parent = this;
	_next->tokenId = tokenId + 1;
}

float Arm::getAngle()
{
	return angle;
}

Arm* Arm::getNext()
{
	return _next;
}

void Arm::setAngle(float a)
{
	angle = a;
	if(_next != NULL)
	{
		_next->setAngle(a);
	}
}

void Arm::wave(float _angle, float _direct, int& _turn)
{
	if(_turn == tokenId)
	{
		delay += 1;		
		if(delay > 14 - tokenId)
		{
			delay = 0;
			_k = _direct;
			_turn = (_turn + 1) % 5;
		}
	}
	_box->_position.x = _parent->_box->_position.x + 32 * cos(_angle) ;
	_box->_position.y = _parent->_box->_position.y + 32 * sin(_angle) ;	
	if(_next != NULL)
		_next->wave(_angle  - (ROTATE_DES)*_k + delay * (3.14f / 30.0f)*_k ,_direct,_turn);
}

void Arm::follow(float _angle)
{
	if(angle > _angle)
	{
		angle = _angle - 0.008f;
	}
	else
	{
		angle = _angle + 0.008f;
	}
	_box->_position.x = _parent->_box->_position.x + 32 * cos(angle) ;
	_box->_position.y = _parent->_box->_position.y + 32 * sin(angle) ;	
	if(_next != NULL)
	{
		_next->follow(_angle);
	}
}

void Arm::rotate(float _angle, float _direct)
{
	_box->_position.x = _parent->_box->_position.x + 32 * cos(_angle) ;
	_box->_position.y = _parent->_box->_position.y + 32 * sin(_angle) ;	
	if(_next!= NULL)
	{		
		_next->rotate(_angle  - (ROTATE_DES)*_direct,_direct);
	}
}

void Arm::kill()
{
	_isAlive = false;
}

void Arm::_setPosition(D3DXVECTOR2 pos)
{
	_box->_position = pos;
}


